from django.db.models import Model
from django.http import Http404, HttpResponse
from django.shortcuts import render, get_object_or_404
from django.urls import reverse

from myapp3.models import BookList

def index(request):
    url = reverse("myapp3:index1")
    # url = reverse("index1")
    return render(request, 'index.html', {'url': url})



# Create your views here.
def getList(request, pk=1):
# def getList(request,category, pk=1):
# def getList(request,category):
    """
    处理列表页请求的视图函数
    参数:
        request: HTTP请求对象，包含客户端请求信息

    返回值:
        HttpResponse对象，返回列表页内容
    """
    # books = BookList.objects.all() # 查询所有图书

    try:
        books = get_object_or_404(BookList, pk=pk) # 查询指定id的图书
        # books = BookList.objects.get(id=1)
        # books=BookList.objects.raw('SELECT * FROM myapp3_booklist WHERE id = %s', [pk])
        # 反向解析URL地址：reverse(视图名称,args参数)
        url=reverse('booksID', args=[pk])

        return render(request, "list.html", {'books': books, 'id':pk,'url':url})
        # return render(request, "list.html", {'books': books, 'id':pk,'category':category})
        # return render(request, "list.html", {'books': books,'category':category})
    except BookList.DoesNotExist:
        return Http404
